package cn.tedu._03vehicle.controller;

import cn.tedu._03vehicle.base.response.JsonResult;
import cn.tedu._03vehicle.base.response.StatusCode;
import cn.tedu._03vehicle.mapper.VehicleMapper;
import cn.tedu._03vehicle.pojo.dto.VehicleAddParam;
import cn.tedu._03vehicle.pojo.dto.VehicleListQuery;
import cn.tedu._03vehicle.pojo.entity.Vehicle;
import cn.tedu._03vehicle.pojo.vo.ListVehicleVO;
import cn.tedu._03vehicle.pojo.vo.UserVO;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;
@Validated
@Slf4j
@Api(tags = "02.车辆管理模块")
@RestController
@RequestMapping("/v1/vehicle")
public class VehicleController {
    @Autowired
    private VehicleMapper vehicleMapper;
    @PostMapping("add")
    @ApiOperation("添加车辆功能")
    @ApiOperationSupport(order = 10)
    public JsonResult addVehicle(@RequestBody VehicleAddParam vehicleAddParam, @ApiIgnore HttpSession session) {
        log.debug("vehicleAddParam ="+ vehicleAddParam);
        //先校验用户是否登录
        //先获取浏览器的cookie存储的用户标识，因为存储的是session
            UserVO userVO = (UserVO) session.getAttribute("user777888XXX");
            Vehicle vehicle = new Vehicle();
            //复制属性
            BeanUtils.copyProperties(vehicleAddParam,vehicle);
            vehicle.setCreateTime(new Date());
            vehicle.setUpdateTime(new Date());
            //在key中再去获取用户的id
            vehicle.setUserId(userVO.getId());
            log.debug("vehicle = "+ vehicle);
            int rows =vehicleMapper.insertVehicle(vehicle);
            return JsonResult.ok();

    }

    @GetMapping("list")
    @ApiOperation("查询车辆列表功能")
    @ApiOperationSupport(order = 20)
    public JsonResult listVehicle(VehicleListQuery vehicleListQuery, @ApiIgnore HttpSession session) {
            log.debug("vehicleListQuery = "+ vehicleListQuery);
            UserVO userVO = (UserVO) session.getAttribute("user777888XXX");
            //获取用户id
            Long userId = userVO.getId();
            String brand = vehicleListQuery.getBrand();
            String license = vehicleListQuery.getLicense();
            List<ListVehicleVO> listVehicleVOS = vehicleMapper.listVehicle(userId, brand, license);
            return JsonResult.ok(listVehicleVOS);

    }
    @DeleteMapping("delete/{id}")
    @ApiOperation("删除车辆功能")
    @ApiOperationSupport(order = 30)
    public JsonResult deleteVehicle(@Range(min = 1,max = 100,message = "车辆编号必须在0-100之间") @PathVariable Long id, @ApiIgnore HttpSession session) {
        //手动抛出一个异常，用于演示全局异常处理器的用法
        if (id <= 0) {
            throw new IllegalArgumentException("id值非法");
        }
        log.debug("id = "+ id);
        //先获取登录的状态
        UserVO userVO = (UserVO) session.getAttribute("user777888XXX");
        log.debug("用户已登录");
            //获取用户id
        Long userId = userVO.getId();
        int rows= vehicleMapper.deleteVehicle(id,userId);
        if (rows == 0) {
            //return new JsonResult(StatusCode.OPERATION_FAILED);
            return new JsonResult(StatusCode.ILLEGAL_REQUEST);
        }else {
            return JsonResult.ok();
        }

    }
}
